//
//  YsFileBrowserManager.m
//  YsFileBrowser
//
//  Created by 宋宏康 on 2020/3/17.
//

#import "YsFileBrowserManager.h"
#import "YsFileBrowserVideoVC.h"
#import "YsFileBrowserQLPreViewVC.h"
#import <GKPhotoBrowser/GKPhotoBrowser.h>
#import <SSZipArchive/SSZipArchive.h>
#import <YsToolsManager/YsToolsManager.h>

@implementation YsFileBrowserManager
+ (void)browserFileType:(YsFileBrowserFileType)fileType
  withCurentVC:(UIViewController *)vc
withDataSource:(NSArray <NSString *>*)dataSource
{
    NSParameterAssert(dataSource);
    NSParameterAssert(vc);

    if (fileType == YsFileBrowserPicFileType || fileType == YsFileBrowserGifFileType) {
        [self browserPicWithDataSource:dataSource withCurentVC:vc];
    }else if (fileType == YsFileBrowserVideoFileType || fileType == YsFileBrowserMp3FileType){
        [self browserVideoWithDataSource:dataSource withCurentVC:vc];
    }else if (fileType == YsFileBrowserQLPreviewFileType){
        [self browserQLPreviewWithDataSource:dataSource withCurentVC:vc];
    }else if (fileType == YsFileBrowserSmartPenFileType){
        [self browserSmartFileWithDataSource:dataSource withCurentVC:vc];
    }
}

+ (void)browserPicWithDataSource:(NSArray <NSString *>*)dataSource   withCurentVC:(UIViewController *)vc
{
     NSMutableArray *photos = [NSMutableArray new];
     [dataSource enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
         GKPhoto *photo = [GKPhoto new];
         photo.url = [NSURL fileURLWithPath:obj];
        [photos addObject:photo];
     }];
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:0];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate;
    [browser showFromVC:vc];
}

+ (void)browserVideoWithDataSource:(NSArray <NSString *>*)dataSource  withCurentVC:(UIViewController *)vc
{
    YsFileBrowserVideoVC *videoVc = [[YsFileBrowserVideoVC alloc] initWithDataSource:dataSource];
     videoVc.modalPresentationStyle = UIModalPresentationFullScreen;
        
    [vc presentViewController:videoVc animated:YES completion:nil];
}

+ (void)browserQLPreviewWithDataSource:(NSArray <NSString *>*)dataSource  withCurentVC:(UIViewController *)vc
{
    YsFileBrowserQLPreViewVC *previewVC = [[YsFileBrowserQLPreViewVC alloc] init];
    [previewVC previewFileWithPaths:dataSource on:vc jump:YsFileBrowserJumpPresentAnimation];
}

+ (void)browserSmartFileWithDataSource:(NSArray <NSString *>*)dataSource  withCurentVC:(UIViewController *)vc
{
    NSMutableArray <NSString *> *unZipPath = [NSMutableArray array];
    NSString *destinatonPath = YsToolsManagerFileUtil.getTmpPath;
//     NSString *zipFileAtPath = [dataSource.lastObject stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *zipFileAtPath = dataSource.lastObject;
    NSString *fileAtPathLastPathComponent = [dataSource.lastObject.lastPathComponent componentsSeparatedByString:@"."].firstObject;
    ///
    BOOL unizipStatus =  [SSZipArchive unzipFileAtPath:zipFileAtPath toDestination:destinatonPath];
    NSString *contentFilePath = [destinatonPath stringByAppendingPathComponent:fileAtPathLastPathComponent];
    if (unizipStatus) {
        NSArray <NSString *> *contentArray =  [YsToolsManagerFileUtil getContentsOfDirectoryAtPath:contentFilePath];
        [contentArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [unZipPath addObject:[contentFilePath stringByAppendingPathComponent:obj]];
        }];
        if (contentArray.count > 0) {
            [self browserFileType:YsFileBrowserPicFileType withCurentVC:vc withDataSource:unZipPath];
        }else{
            NSLog(@"解压失败");
        }
    }else{
        NSLog(@"解压失败");
    }
}
@end
